package com.zyx.tl.transmittable.transmittabletl;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.threadpool.TtlExecutors;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * <p>TransmittableThreadLocal可以解决InheritableThreadLocal线程池中获取线程数据被覆盖的问题</p>
 *
 * @author Yaxi.Zhang
 * @since 2022/8/18 08:52
 */
public class CustomThreadLocal {
    static TransmittableThreadLocal<String> threadLocal = new TransmittableThreadLocal<>();
    static ExecutorService pool =  TtlExecutors.getTtlExecutorService(Executors.newFixedThreadPool(2));

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            int j = i;
            pool.execute(new Thread(() -> {
                CustomThreadLocal.threadLocal.set("zyx" + j);
                new Service().call();
            }));
        }
    }
}
